#coding = utf-8
import cv2 as cv
import numpy as np

filename = r'LENA.jpg'

img = cv.imread(filename)
img1 = cv.resize(img, (400,400), interpolation=cv.INTER_LANCZOS4)
gray = cv.cvtColor(img1, cv.COLOR_BGR2GRAY)

#########     Sobel算子边缘检测    #############
x = cv.Sobel(gray, cv.CV_16S, 1, 0)
y = cv.Sobel(gray, cv.CV_16S, 0, 1)
adsX = cv.convertScaleAbs(x)
adsY = cv.convertScaleAbs(y)
dst = cv.addWeighted(adsX, 0.5, adsY, 0.5, 0)

cv.imshow("adsX", adsX)
cv.imshow("adsY", adsY)
cv.imshow("Sobel_Result", dst)

# cv.waitKey()
# cv.destroyAllWindows()
################################################

############  Canny算子边缘检测   ##############
canny = cv.Canny(gray, 80, 200)

def nothing(x):
    pass
cv.namedWindow('res')
cv.createTrackbar('min', 'res', 0, 25, nothing)
cv.createTrackbar('max', 'res', 0, 25, nothing)
while(1):
    if cv.waitKey(1)&0xFF==27:
        break
    maxVal=cv.getTrackbarPos('max', 'res')
    minVal=cv.getTrackbarPos('min', 'res')
    canny=cv.Canny(img, 10*minVal, 10*maxVal)
    cv.imshow('res', canny)
    cv.waitKey()
    cv.destroyAllWindows()

# cv.imshow("canny image", canny)

cv.waitKey()
cv.destroyAllWindows()
